package com.md24housecall.manager;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.widget.Toast;
import com.md24housecall.activity.R;
import com.md24housecall.util.GlobalConstant;
import com.md24housecall.util.MyLog;

public class SkypeManager {
	private static SkypeManager mInstance;
	private GlobalConstant constant = GlobalConstant.getInstance();
	private final Date currentTime = new Date();
	@SuppressLint("SimpleDateFormat")
	private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

	public static SkypeManager getInstance() {
		if (mInstance == null)
			mInstance = new SkypeManager();
		return mInstance;
	}

	public String getSkypename(Context myContext) {
		try {
			AccountManager manager = AccountManager.get(myContext);
			Account[] accounts = manager
					.getAccountsByType("com.skype.contacts.sync");
			List<String> possibleSkype = new LinkedList<String>();

			for (Account account : accounts) {
				possibleSkype.add(account.name);
			}
			return possibleSkype.get(0);
		} catch (Exception e) {
			// TODO: handle exception
			MyLog.d(GlobalConstant.TAG, "exception get skype name: "+e.getMessage());
			return "";
		}
	}

	// Determine whether the Skype for Android client is installed on this
	// device.
	public boolean checkSkypeInstalled(Context myContext) {
		PackageManager myPackageMgr = myContext.getPackageManager();
		try {
			myPackageMgr.getPackageInfo("com.skype.raider",
					PackageManager.GET_ACTIVITIES);
		} catch (PackageManager.NameNotFoundException e) {
			return false;
		}
		return true;
	}

	// Install the Skype client through the market: URI scheme.
	public void goToMarket(Context myContext) {
		Uri marketUri = Uri.parse("market://details?id=com.skype.raider");
		Intent myIntent = new Intent(Intent.ACTION_VIEW, marketUri);
		myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		myContext.startActivity(myIntent);
	}

	private void initCallInfo(String toSkype,
			Context myContext) {
//		constant.callFrom = getSkypename(myContext);
		constant.callTo = toSkype;
		// Give it to me in GMT time.
		// sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
		constant.callTime = sdf.format(currentTime);
		constant.startTime = System.currentTimeMillis();
	}

	// call skype
	public void callViaSkype(Context myContext, String toSkypeName) {
		String skypeName = getSkypename(myContext);
		constant.isLoginSkype=true;
		try {
			// check skype installed?
			if (!checkSkypeInstalled(myContext)) {
				Toast.makeText(myContext,myContext.getResources().getString(
								R.string.msg_skype_not_install),Toast.LENGTH_LONG).show();
				goToMarket(myContext);
				constant.isLoginSkype=false;
				return;
			} 
			else if (skypeName.equals("")) {
				Toast.makeText(myContext,myContext.getResources().getString(
								R.string.msg_skype_account_not_found),Toast.LENGTH_LONG).show();
				constant.isLoginSkype=false;
				focusSkype(myContext);
				return;
			}
			initCallInfo(toSkypeName, myContext);
			String videoCallSkypeUri= "skype:%s?call&video=true";
			videoCallSkypeUri = String.format(videoCallSkypeUri, toSkypeName);
			Intent skype_intent = new Intent("android.intent.action.VIEW");
			skype_intent.setClassName("com.skype.raider",
					"com.skype.raider.Main");
			skype_intent.setData(Uri.parse(videoCallSkypeUri));
//			skype_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			myContext.startActivity(skype_intent);
		} catch (Exception e) {
			MyLog.d(GlobalConstant.TAG, "call skype exception: "+e.getMessage());
		}
	}
	
	public void focusSkype(Context myContext) {
		try {
			// check skype installed?
			if (!checkSkypeInstalled(myContext)) {
				Toast.makeText(myContext,myContext.getResources().getString(
								R.string.msg_skype_not_install),Toast.LENGTH_LONG).show();
				goToMarket(myContext);
				constant.isLoginSkype=false;
				return;
			} 
			String videoCallSkypeUri= "skype:";
			Intent skype_intent = new Intent("android.intent.action.VIEW");
			skype_intent.setClassName("com.skype.raider",
					"com.skype.raider.Main");
			skype_intent.setData(Uri.parse(videoCallSkypeUri));
			myContext.startActivity(skype_intent);
		} catch (Exception e) {
			MyLog.d(GlobalConstant.TAG, "focus skype exception: "+e.getMessage());
		}
	}
}
